import matplotlib.pyplot as plt
import pandas as pd
# 设置中文字体和解决负号显示问题
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['font.size'] = 14
# 读取CSV文件中的年龄数据
df = pd.read_csv('./data/ages_distribution.csv')
# 按年龄段划分人群
youth = df[(df['年龄'] >= 18) & (df['年龄'] <= 35)]['年龄']
middle_aged = df[(df['年龄'] >= 36) &
                                       (df['年龄'] <= 59)]['年龄']
senior = df[df['年龄'] >= 60]['年龄']
# 构造绘图数据
data = [youth, middle_aged, senior]
labels = ['青年 (18-35岁)', '中年 (36-59岁)', '老年 (≥60岁)']
# 绘制箱型图
plt.boxplot(data, tick_labels=labels, patch_artist=True,
       medianprops={'color': 'red'},
       boxprops=dict(facecolor='lightblue',
                                         color='blue'),
       flierprops=dict(marker='o', markerfacecolor='gold',
                                         markersize=5))
# 添加标题和坐标轴标签
plt.title('不同年龄段居民年龄分布箱型图')
plt.ylabel('年龄')
# 显示网格
plt.grid(True, axis='y', linestyle='--', alpha=0.7)
plt.savefig('./Figures/boxplot09-5.png')
plt.show()
